<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <div class="title">{{ projectInfo.projectName }}</div>
    </el-row>
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="staffName">
        <el-input v-model="queryParams.staffName" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="归档状态" prop="contractFile">
        <el-select v-model="queryParams.contractFile" placeholder="请选择" clearable @keyup.enter.native="handleQuery">
          <el-option label="已归档" value="1"></el-option>
          <el-option label="未归档" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="staffContractList" @selection-change="handleSelectionChange" border>
      <el-table-column label="员工编号" align="center" prop="staffContractId" />
      <el-table-column label="姓名" align="center" prop="staffName" />
      <el-table-column label="职务" align="center" prop="projectDuitsName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_duits" :value="scope.row.projectDuitsName" />
        </template>
      </el-table-column>
      <el-table-column label="合同编号" align="center" prop="staffContractNo" />
      <el-table-column label="合同开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="contractFile">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.contractFile == 1">已归档</el-tag>
          <el-tag v-if="scope.row.contractFile == 2" type="danger">未归档</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="合同状态" align="center" prop="contractStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.contractStatus == 1">履约中</el-tag>
          <el-tag v-if="scope.row.contractStatus == 2" type="danger">已到期</el-tag>
          <el-tag v-if="scope.row.contractStatus == 3" type="danger">已失效</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
            v-hasPermi="['cld:staffContract:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改人事合同对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="80px">
        <el-form-item label="是否归档" prop="contractFile">
          <el-select v-model="form.contractFile" placeholder="请选择">
            <el-option v-for="item in contractFileOptions" :key="item.value" :label="item.label"
              :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合同附件" prop="attachment">
          <file-upload v-model="form.attachment" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStaffContract, getStaffContract, delStaffContract, addStaffContract, updateStaffContract } from "@/api/cld/staffContract";
import { getProject } from '@/api/cld/project'
import { contractFileOptions } from '@/assets/staticDict'
export default {
  name: "StaffContract",
  dicts: ['cld_project_duits'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人事合同表格数据
      staffContractList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: this.$route.params.projectId,
        staffName: null,
        contractStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      projectInfo: {
        projectName: ''
      },
      // 归档状态
      contractFileOptions: contractFileOptions
    };
  },
  created() {
    getProject(this.$route.params.projectId).then(res => {
      this.projectInfo = res.data;
    })
    this.getList();
  },
  methods: {
    /** 查询人事合同列表 */
    getList() {
      this.loading = true;
      listStaffContract(this.queryParams).then(response => {
        this.staffContractList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        staffContractId: null,
        projectId: null,
        staffContractNo: null,
        startTime: null,
        endTime: null,
        attachment: null,
        staffContratName: null,
        createId: null,
        createName: null,
        createTime: null,
        flag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人事合同";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const staffContractId = row.staffContractId || this.ids
      getStaffContract(staffContractId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改人事合同";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.staffContractId != null) {
            updateStaffContract(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStaffContract(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 返回
    handleReturn() {
      const obj = { path: "/cloudManage/personnel/staffContract", name: "staffContract" };
      this.$tab.closeOpenPage(obj);
      this.$tab.refreshPage(obj);
    }
  }
};
</script>
<style scoped>
.title {
  text-align: left;
  font-size: 20px;
  font-weight: 600;
}
</style>
